#include "stm32f10x.h"
#include "MySPI.h"
void W25Q64_Init(void)
{
    MySPI_Init();
}

// 获取w25Q64的ID
void W25Q64_GetID(uint8_t *MID, uint16_t *DID)
{
    MySPI_Start();
    MySPI_swap(0x9F);
    *MID = MySPI_swap(0xFF);
    *DID = MySPI_swap(0xFF);
    *DID <<= 8;
    *DID |= MySPI_swap(0xFF);
    MySPI_Stop();
}

// 写使能
void write_ID(void)
{
    MySPI_Start();
    MySPI_swap(0x06);
    MySPI_Stop();
}
// 状态寄存器是否在忙，。如果是，等待
void read_status()
{
    MySPI_Start();
    MySPI_swap(0x05);
    int Time = 10000;
    while (MySPI_swap(0xFF) & 0x01 == 0x01)
    {
        Time--;
        if (Time == 0)
        {
            break;
        }
    };
    MySPI_Stop();
}
// 页编程
void w225PageProgram(uint32_t Adress, uint32_t *DataArray, uint16_t Count)
{
    write_ID();
    MySPI_Start();
    MySPI_swap(0x02);
    MySPI_swap(Adress >> 16);
    MySPI_swap(Adress >> 8);
    MySPI_swap(Adress);
    for (int i = 0; i < Count; i++)
    {
        MySPI_swap(DataArray[i]);
    }
    MySPI_Stop();
    read_status();
}
// count表示行数
void w225PageProgram2(uint32_t Adress, char Data[][16], uint16_t Count)
{
    write_ID();
    MySPI_Start();
    MySPI_swap(0x02);
    MySPI_swap(Adress >> 16);
    MySPI_swap(Adress >> 8);
    MySPI_swap(Adress);

    for (int i = 0; i < Count; i++)
    {
        for (int j = 0; j < 16; j++)
        {
            MySPI_swap(Data[i][j]);
        }
    }
    MySPI_Stop();
    read_status();
}
// 扇区擦除
void w25SectorErase(uint32_t Adress)
{
    write_ID();
    MySPI_Start();
    MySPI_swap(0x20);
    MySPI_swap(Adress >> 16);
    MySPI_swap(Adress >> 8);
    MySPI_swap(Adress);
    MySPI_Stop();
    read_status();
}

// 读
void w25read(uint32_t Adress, uint8_t *DataArray, uint32_t Count)
{
    MySPI_Start();
    MySPI_swap(0x03);
    MySPI_swap(Adress >> 16);
    MySPI_swap(Adress >> 8);
    MySPI_swap(Adress);

    for (uint32_t i = 0; i < Count; i++)
    {
        DataArray[i] = MySPI_swap(0xFF);
    }
    MySPI_Stop();
}
    // count表示行数
    void w25read2(uint32_t Adress, char Data[][16], uint32_t Count)
    {
        MySPI_Start();
        MySPI_swap(0x03);
        MySPI_swap(Adress >> 16);
        MySPI_swap(Adress >> 8);
        MySPI_swap(Adress);

        for (uint8_t i = 0; i < Count; i++)
        {
            for (uint8_t j = 0; j < 16; j++)
            {
                Data[i][j] = MySPI_swap(0xFF);
            }
        }
        MySPI_Stop();
    }